package binary_tree.leetcode.easy;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

/**
 * @author Stark
 * @description 二叉树的最大深度
 * 题目链接：https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
 * @date 2022/4/16 22:59
 **/
public class Num104_MaxDepth {
    int choice = 1;
    public int maxDepth(TreeNode root) {
        //方法一；递归
        if(choice == 1) {
            if (root == null) {
                return 0;
            }
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
        //方法二：非递归
        //使用队列
        if(choice == 2){
            if(root == null)
                return  0;
            Queue<TreeNode> queue = new ArrayDeque<>();
            queue.offer(root);
            int count = 0;
            while(!queue.isEmpty()){
                count += 1;
                int size = queue.size();
                for(int i = 0; i < size;i++){
                    TreeNode node = queue.poll();
                    if(node.left != null)
                        queue.offer(node.left);
                    if(node.right != null)
                        queue.offer(node.right);
                }
            }
            return count;
        }
        return 0;
    }
}
